package com.lcj.dr.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcj.dr.controller.form.CanPayForm;
import com.lcj.dr.controller.form.PayFineForm;
import com.lcj.dr.controller.form.SearchFineForm;
import com.lcj.dr.db.dao.DriverFineDao;
import com.lcj.dr.db.dao.WalletDao;
import com.lcj.dr.db.pojo.DriverFineEntity;
import com.lcj.dr.service.FineService;
import io.micrometer.core.instrument.search.Search;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FineServiceImpl implements FineService {

    @Resource
    private DriverFineDao fineDao;

    @Resource
    private WalletDao walletDao;

    /**
     * 分页查询司机罚款
     * @param form
     * @return PageInfo<DriverFineEntity>
     */
    @Override
    public PageInfo<DriverFineEntity> searchFineByPage(SearchFineForm form) {
        PageHelper.startPage(form.getPage(), form.getLength());
        List<DriverFineEntity> list = fineDao.searchFine(form);
        PageInfo<DriverFineEntity> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 能否支付罚款
     * @param form
     * @return HashMap
     */
    @Override
    public HashMap canPay(CanPayForm form) {
        SearchFineForm fineForm = new SearchFineForm();
        fineForm.setId(fineForm.getId());
        List<DriverFineEntity> fine = fineDao.searchFine(fineForm);
        BigDecimal amount = BigDecimal.valueOf(0x3f3f3f3f);
        if (fine.size() > 0) {
            DriverFineEntity fineEntity = fine.get(0);
            amount = fineEntity.getAmount();
        }

        Map map = walletDao.selectWallet(form.getDriverId());
        String password = MapUtil.getStr(map, "password");
        Double balance = MapUtil.getDouble(map, "balance");
        HashMap<String, Object> res = new HashMap();
        res.put("hasPassword", StringUtils.isNotBlank(password));
        res.put("canPay", balance > amount.doubleValue());
        return res;
    }

    /**
     * 支付罚款
     * @param form
     * @return
     */
    @Override
    @Transactional
    public Integer payFine(PayFineForm form) {
        Map<String, Object> param = new HashMap();
        param.put("password", form.getPassword());
        if(form.getAmount() > 0) {
            form.setAmount(form.getAmount() * -1);
        }
        param.put("amount", form.getAmount());
        param.put("driverId", form.getDriverId());
        int rows = walletDao.updateWalletBalance(param);
        if(rows > 0){
            param.clear();
            param.put("id", form.getFineId());
            param.put("status", 2);
            fineDao.updateFine(param);
        }
        return rows;
    }
}
